using AutoRegister.Tests.Lib;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using Xunit;

namespace AutoRegister.Tests
{
    public class Registers
    {
        [Fact]
        public void ReferenceAssembly()
        {
            var serviceCollection = new ServiceCollection();

            var configuration = new ConfigurationBuilder().AddInMemoryCollection().Build();

            serviceCollection.AddAutoRegister(configuration, options =>
            {
                options.Matches.Add("AutoRegister.Tests.*");
            });

            using (var serviceProvider = serviceCollection.BuildServiceProvider())
            {
                using (var scopeProvider = serviceProvider.CreateScope())
                {
                    var tests = scopeProvider.ServiceProvider.GetServices<ILibTest>().Select(item=>item.GetLibName()).ToList();

                    Assert.Contains("A", tests);
                }
            }
        }

        [Fact]
        public void ImportAssembly()
        {
            var serviceCollection = new ServiceCollection();

            var configuration = new ConfigurationBuilder().AddInMemoryCollection().Build();

            serviceCollection.AddAutoRegister(configuration, options =>
            {
                options.Imports.Add("AutoRegister.Tests.LibB.dll");
                options.Matches.Add("AutoRegister.Tests.*");
            });

            using (var serviceProvider = serviceCollection.BuildServiceProvider())
            {
                using (var scopeProvider = serviceProvider.CreateScope())
                {
                    var tests = scopeProvider.ServiceProvider.GetServices<ILibTest>().Select(item => item.GetLibName()).ToList();

                    Assert.Contains("B", tests);
                }
            }
        }
    }
}
